package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.common.BaseServiceImpl;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends BaseServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.检查参数
        if(dto==null)return errorResult;
        Page pageParam = new Page(dto.page,dto.size);
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage page = page(pageParam, wrapper);
        PageResponseResult result = new PageResponseResult(dto.page,dto.size,(int)page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数检查
        if(adSensitive == null)return errorResult;
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return sucResult;
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.参数检查
        if(adSensitive == null || adSensitive.getId() == null)return errorResult;
        updateById(adSensitive);
        return sucResult;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.参数检查
        if(id == null)return errorResult;
        AdSensitive byId = getById(id);
        if(byId == null)return noData;
        //3.删除操作
        removeById(id);
        return sucResult;
    }
}